package com.wdf.dota;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.wdf.bean.DotaHeros;
import com.wdf.helper.FileUtils;

public class StrategyActivity extends Activity {

	private  ArrayList<HashMap<String, Object>> banks = new ArrayList<HashMap<String, Object>>();

	int times = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.strategylist);
		ListView listview = (ListView) findViewById(android.R.id.list);
		
		SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
				Context.MODE_PRIVATE);
		times = sharedPreferences.getInt("times", 0);
		
		
		initBanks();
		SimpleAdapter adapter = new SimpleAdapter(this, banks,
				R.layout.strategyitem, new String[] { "img", "strategyname" },
				new int[] { R.id.img, R.id.strategyname });
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch (arg2) {

				case 0:
					Intent intent0 = new Intent(StrategyActivity.this,
							BaseActivity.class);
					startActivityForResult(intent0, 0);
					break;

				case 1:
					Intent intent1 = new Intent(StrategyActivity.this,
							ShuyuActivity.class);
					startActivityForResult(intent1, 0);
					break;
				case 2:
					Intent intent2 = new Intent(StrategyActivity.this,
							CommandActivity.class);
					startActivityForResult(intent2, 0);
					break;
				case 3:
					type = 1;
					showDialog(StrategyActivity.this);
					break;
				case 4:
					type = 2;
					showDialog(StrategyActivity.this);
					break;
				case 5:
					type = 3;
					showDialog(StrategyActivity.this);
					break;

				}
			}
		});
	

	}

	public void initBanks() {
		HashMap<String, Object> bank0 = new HashMap<String, Object>();
		bank0.put("strategyname", "Dota通用技巧");
		bank0.put("img", R.drawable.pub01);
		banks.add(bank0);

		HashMap<String, Object> bank1 = new HashMap<String, Object>();
		bank1.put("strategyname", "Dota专业术语");
		bank1.put("img", R.drawable.pub02);
		banks.add(bank1);

		HashMap<String, Object> bank2 = new HashMap<String, Object>();
		bank2.put("strategyname", "Dota常用命令");
		bank2.put("img", R.drawable.pub03);
		banks.add(bank2);


			HashMap<String, Object> bank4 = new HashMap<String, Object>();
			bank4.put("strategyname", "导出英雄头像");
			bank4.put("img", R.drawable.pub04);
			banks.add(bank4);

			HashMap<String, Object> bank5 = new HashMap<String, Object>();
			bank5.put("strategyname", "导出英雄技能");
			bank5.put("img", R.drawable.pub05);
			banks.add(bank5);

			HashMap<String, Object> bank6 = new HashMap<String, Object>();
			bank6.put("strategyname", "导出装备图片");
			bank6.put("img", R.drawable.pub07);
			banks.add(bank6);
			
		

		
	}
	private ProgressDialog dialog;

	private void exportHeroImage(Context context) {
		Handler handler = new Handler();
		Runnable mTasks = new Runnable() {
			public void run() {
				prepare();
				storeBitmapIntoSDcard();
				dialog.dismiss();
			}
		};
		dialog = new ProgressDialog(this.getParent());
		dialog.setIcon(R.drawable.smile);
		dialog.setTitle("请稍等");
		dialog.setMessage("正在导出图片到手机SD卡.....");
		dialog.setCancelable(true);
		dialog.show();
		handler.postDelayed(mTasks, 1000);

	}

	private void storeBitmapIntoSDcard() {
		int result_code = 0;
		try {
			saveImpageInSD();
			result_code = 1;
		} catch (Exception e) {
			result_code = -1;
		}
		if (result_code == 1) {
			Toast.makeText(StrategyActivity.this,
					"恭喜您！图片导出成功,\n重启手机后，可在本地相册中浏览！", 1).show();
		} else {
			Toast.makeText(StrategyActivity.this, "请检查SD卡及相应权限设置", 1).show();
		}
	}

	String path = FileUtils.getSDCardPath() + "Dota/";
	Bitmap[] images = new Bitmap[0];
	String[] names = new String[0];

	private void saveImpageInSD() {
		for (int i = 0; i < images.length; i++) {
			storeBitmapInSD(names[i], images[i]);
		}
	}

	private boolean storeBitmapInSD(String filename, Bitmap bitmap) {
		if ("".equals(path)) {
			return false;
		}
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		File imageFile = new File(file, filename + ".jpg");
		try {
			imageFile.createNewFile();
			FileOutputStream fos = new FileOutputStream(imageFile);
			bitmap.compress(CompressFormat.JPEG, 50, fos);
			fos.flush();
			fos.close();
			return true;
		} catch (FileNotFoundException e) {

			return false;

		} catch (IOException e) {

			return false;
		}
	}

	private void showDialog(final Context context) {
		
		exportHeroImage(StrategyActivity.this);
	}

	int type = 0;

	public void prepare() {
		switch (type) {
		case 1:
			images = new Bitmap[DotaHeros.heros.length];
			names = new String[DotaHeros.heros.length];
			for (int i = 0; i < DotaHeros.heros.length; i++) {
				int resID = getResources().getIdentifier(DotaHeros.heros[i],
						"drawable", "com.wdf.dota");
				images[i] = BitmapFactory.decodeResource(getResources(), resID);
				names[i] = DotaHeros.heros[i];
			}
			break;
		case 2:
			images = new Bitmap[DotaHeros.heros.length * 4];
			names = new String[DotaHeros.heros.length * 4];
			for (int i = 0; i < DotaHeros.heros.length; i++) {
				for (int j = 1; j <= 4; j++) {
					String heroname = DotaHeros.heros[i];
					String skill1Path = heroname + "_skill1.png";
					String skill2Path = heroname + "_skill2.png";
					String skill3Path = heroname + "_skill3.png";
					String skill4Path = heroname + "_skill4.png";

					try {
						InputStream fis1 = getAssets().open(skill1Path);
						images[i * 4] = BitmapFactory.decodeStream(fis1);
						names[i * 4] = heroname + "_skill1";
						InputStream fis2 = getAssets().open(skill2Path);
						images[i * 4 + 1] = BitmapFactory.decodeStream(fis2);
						names[i * 4 + 1] = heroname + "_skill2";
						InputStream fis3 = getAssets().open(skill3Path);
						images[i * 4 + 2] = BitmapFactory.decodeStream(fis3);
						names[i * 4 + 2] = heroname + "_skill3";
						InputStream fis4 = getAssets().open(skill4Path);
						images[i * 4 + 3] = BitmapFactory.decodeStream(fis4);
						names[i * 4 + 3] = heroname + "_skill4";
					} catch (IOException e) {
						return;
					}
				}
			}
			break;
		case 3:
			List<Bitmap> bitmaps = new ArrayList<Bitmap>();
			List<String> filenames = new ArrayList<String>();
			for (int i = 1; i <= 11; i++) {
				for (int j = 1; j <= 12; j++) {
					if ((i == 2 && j == 11) || (i == 8 && j == 11)
							|| (i == 8 && j == 12))
						continue;
					filenames.add("store" + i + "_" + j);
					int resID = getResources().getIdentifier(
							"store" + i + "_" + j, "drawable", "com.wdf.dota");
					bitmaps.add(BitmapFactory.decodeResource(getResources(),
							resID));
				}
			}
			images = bitmaps.toArray(new Bitmap[bitmaps.size()]);
			names = filenames.toArray(new String[filenames.size()]);
			break;

		}
	}
}